package com.park.web.controller.admin;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.park.client.model.app.AppVersionModel;
import com.park.client.model.pub.VersionModel;
import com.park.client.service.app.AppVersionService;
import com.park.client.service.pub.VersionService;
import com.park.common.constant.Constant;
import com.park.common.file.FileUtil;
import com.park.common.util.Page;
import com.park.common.util.PageData;
import com.park.common.util.Result;
import com.park.web.contant.Global;
import com.park.web.controller.BaseController;

import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping("/admin/log/version")
@Slf4j
public class AdminVersionController extends BaseController{

	@Autowired
	private VersionService versionService;
	@Autowired
	private AppVersionService appVersionService;
	
	@RequestMapping("versionList")
	public String newsList(HttpServletRequest request){
			return Global.ADMIN_PATH+"log/version/versionList";
	}
	
	@RequestMapping("save")
	@ResponseBody
	public Result<Boolean> save(VersionModel version,HttpServletRequest request){
		Result<Boolean> json = new Result<>();
		version.setCreateDate(new Date());
		boolean success = versionService.insertSelective(version);
		if(success){
			json.setMessage(Constant.SUCCESS_SAVE_MSG);
			json.setSuccess(Constant.SUCCESS);
		}else{
			json.setSuccess(Constant.FAIL);
			json.setMessage(Constant.FAIL_SAVE_MSG);
		}
		return json;
	}
	
	@RequestMapping("deleteById")
	@ResponseBody
	public Result<Boolean> deleteById(@RequestParam("id") Long id,HttpServletRequest request){
		Result<Boolean> json = new Result<>();
		boolean success = versionService.deleteByPrimaryKey(id);
		if(success){
			json.setMessage(Constant.SUCCESS_DELETE_MSG);
			json.setSuccess(Constant.SUCCESS);
		}else{
			json.setMessage(Constant.FAIL_DELETE_MSG);
			json.setSuccess(Constant.FAIL);
		}
		return json;
	}
	
	@RequestMapping("edit")
	@ResponseBody
	public Result<Boolean> edit(VersionModel version,HttpServletRequest request){
		Result<Boolean> json = new Result<>();
		version.setCreateDate(new Date());
		boolean success = versionService.updateByPrimaryKeySelective(version);
		if(success){
			json.setMessage(Constant.SUCCESS_UPDATE_MSG);
			json.setSuccess(Constant.SUCCESS);
		}else{
			json.setMessage(Constant.FAIL_UPDATE_MSG);
			json.setSuccess(Constant.FAIL);
		}
		return json;
	}
	
	@RequestMapping("uploadApp")
	public String uploadApp(MultipartFile file,AppVersionModel appVersion,HttpServletRequest request){
		if(!file.isEmpty()){
			try {
				InputStream is =  file.getInputStream();
				appVersion.setCreateDate(new Date());
				appVersion.setVersionContent(FileUtil.getBytes(is));
				if(appVersion.getId() != null){
					appVersionService.updateByPrimaryKeySelective(appVersion);
				}else{
					appVersionService.insertSelective(appVersion);
				}
			}catch (Exception e) {
				log.error("上传失败",e);
			}
		}
		return "redirect:appVersionList";
	}
	
	@RequestMapping("appDeleteById")
	@ResponseBody
	public Result<Boolean> appDeleteById(@RequestParam("ids") Long[] ids,HttpServletRequest request){
		Result<Boolean> json = new Result<>();
		try {
			for (int i = 0; i < ids.length; i++) {
				versionService.deleteByPrimaryKey(ids[i]);
			}
			json.setMessage(Constant.SUCCESS_DELETE_MSG);
			json.setSuccess(Constant.SUCCESS);
		} catch (Exception e) {
			json.setMessage(Constant.FAIL_DELETE_MSG);
			json.setSuccess(Constant.FAIL);
		}
		return json;
	}
	
	@RequestMapping("appListRows")
	@ResponseBody
	public PageData<AppVersionModel> listRows(Integer rows,@RequestParam(name="page",defaultValue="1")Integer pageNow,HttpServletRequest request){
		PageData<AppVersionModel> pageData = new PageData<>();
		AppVersionModel appVersion = new AppVersionModel();
		Integer total = appVersionService.selectTotal(appVersion);
		Page page = new Page(pageNow,total,rows);
		appVersion.setStartPos(page.getStartPos());
		appVersion.setPageSize(page.getPageSize());
		List<AppVersionModel> list = appVersionService.selectPagination(appVersion);
		pageData.setTotal(total);
		pageData.setRows(list);
		return pageData;
	}
	
	@RequestMapping("appVersionList")
	public String appVersionList(HttpServletRequest request){
			return Global.ADMIN_PATH+"log/version/appVersionList";
	}
}
